import 'package:flutter_study/project/_01osc/pages/public_tweet_page.dart';
import 'package:flutter_study/project/_01osc/pages/setting_page.dart';
import 'package:flutter_study/project/_01osc/pages/tweet_black_list.dart';
import 'package:flutter_study/project/_01osc/pages/about_page.dart';
import 'package:flutter/material.dart';

class MyDrawer extends StatelessWidget {
  // 头部背景图片路径
  final String headImgPath;

  // 菜单图标
  final List iconList;

  // 菜单文本
  final List<String> menuList;

  // assert在调试时检测
  MyDrawer({Key key, this.headImgPath, this.iconList, this.menuList})
      : assert(headImgPath != null),
        assert(iconList != null),
        assert(menuList != null),
        super(key: key);

  // 页面跳转
  _navToPage(BuildContext context, Widget page) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => page));
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      elevation: 0.0, // 去掉头部阴影
      child: ListView.separated(
          padding: const EdgeInsets.all(0.0), // 解决状态栏不沉浸的问题
          itemBuilder: (context, index) {
            if (index == 0) {
              // 返回头部图片
              return Image.asset(
                headImgPath,
                fit: BoxFit.cover,
              );
            }
            index -= 1;
            return ListTile(
              leading: Icon(iconList[index]),
              title: Text(menuList[index]),
              trailing: Icon(Icons.arrow_forward_ios),
              onTap: () {
                switch (index) {
                  case 0:
                    _navToPage(context, PublicTweetPage());
                    break;
                  case 1:
                    _navToPage(context, TweetBlackListPage());
                    break;
                  case 2:
                    _navToPage(context, AboutPage());
                    break;
                  case 3:
                    _navToPage(context, SettingPage());
                    break;
                }
              },
            );
          },
          separatorBuilder: (context, index) => Divider(
                height: index == 0 ? 0.0 : 1.0,
              ),
          itemCount: menuList.length + 1),
    );
  }
}
